package com.fone.player.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.fone.player.R;

/**
 * 自定义警告对话框
 * 
 * @author kongxiaojun
 * @since 2014-6-12
 */
public class CustomAlertDialog extends Dialog {

	private CustomAlertDialog(Context context, int theme) {
		super(context, theme);
	}

	private CustomAlertDialog(Context context) {
		super(context);
	}

	public static class Builder {
		private Context context;
		private String title;
		private String message;
		private String leftButtonText;
		private String rightButtonText;
		private CustomDialogOnClickListener mlClickListener;

		public static interface CustomDialogOnClickListener {
			/**
			 * 左侧按钮点击回调
			 * 
			 * @return void
			 * @throws
			 */
			public void onLeft();

			/**
			 * 又按钮点击回调
			 * 
			 * @return void
			 * @throws
			 */
			public void onRight();
		}

		/**
		 * @param title
		 *            标题文字显示；
		 * @param message
		 *            dialog文字内容信息；
		 * @param v
		 *            自定义布局，默认null
		 * @param listener
		 *            按钮监听器
		 * @param leftButtonText
		 *            左边按钮文字显示 ，如果传null则不显示这个按钮；
		 * @param rightButtonText
		 *            右边按钮文字显示 ，如果传null则不显示这个按钮；
		 * **/
		public Builder(Context context, CharSequence title, CharSequence message, CharSequence leftButtonText, CharSequence rightButtonText, CustomDialogOnClickListener listener) {
			this.context = context;
			this.title = (String) title;
			this.message = (String) message;
			this.leftButtonText = (String) leftButtonText;
			this.rightButtonText = (String) rightButtonText;
			this.mlClickListener = listener;
		}

		public CustomAlertDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final CustomAlertDialog dialog = new CustomAlertDialog(context, R.style.comm_alertdialog);
			View layout = inflater.inflate(R.layout.custom_alert_dialog_layout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			if (title != null) {
				((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
			}

			// set the confirm button
			if (leftButtonText == null && rightButtonText == null) {
				layout.findViewById(R.id.dialog_bottom_layout).setVisibility(View.GONE);
			}

			if (leftButtonText != null) {
				if (!TextUtils.isEmpty(leftButtonText)) {
					((Button) layout.findViewById(R.id.dialog_left_button)).setText(leftButtonText);
				}
				if (mlClickListener != null) {
					((Button) layout.findViewById(R.id.dialog_left_button)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							if (mlClickListener != null)
								mlClickListener.onLeft();
							dialog.dismiss();
						}
					});
				}
			}
			// set the cancel button
			if (rightButtonText != null) {
				if (!TextUtils.isEmpty(rightButtonText)) {
					((Button) layout.findViewById(R.id.dialog_right_button)).setText(rightButtonText);
				}
				if (mlClickListener != null) {
					((Button) layout.findViewById(R.id.dialog_right_button)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							if (mlClickListener != null)
								mlClickListener.onRight();
							dialog.dismiss();
						}
					});
				}
			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.dialog_content_tv)).setText(message);
			}
			dialog.setContentView(layout);
			return dialog;
		}

	}

}
